﻿#region copyright
//<copyright>
// Copyright(C) 2012 TrackerRealm Corporation
// This file is part of the open source project - Jazz. http://jazz.codeplex.com
// 
// Jazz is open software: you can redistribute it and/or modify it 
// under the terms of the GNU Affero General Public License (AGPL) as published by 
// the Free Software Foundation, version 3 of the License.
// 
// Jazz is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty 
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
//  See the GNU Affero General Public License (AGPL) for more details.
// 
// You should have received a copy of the GNU General Public 
// License along with Jazz.  If not, see <http://www.gnu.org/licenses/>.
//
// REMOVAL OF THIS NOTICE IS VIOLATION OF THE COPYRIGHT. 
//</copyright>
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using TrackerRealm.Jazz.Common;
using C = TrackerRealm.Jazz.Common.JazzConstants;

namespace TrackerRealm.Jazz.Client
{
    /// <summary>
    /// An abstract "<see cref="jObject"/>" class that provides a string Name.
    /// <para>All instances of this class must have a unique name.</para>
    /// <para>Instances of classes that inherit from '<see cref="jNameUnique"/>' will searchable in
    /// 'ClientNexus.Cache.NameObjects'.</para>
    /// <para>Also see '<see cref="jName"/>'.</para>
    /// </summary>
    [UniqueName(true)]
    [Jazz]
    public class jNameUnique:jObject
    {

        #region Constructor
        /// <summary>
        /// Constructor 
        /// <para>Using this constructor will require 1) a static 'CreateEmpty' method in the inheriting class
        /// or 2) changing the settins in 'NexusConfig'.</para>
        /// </summary>
        /// <param name="nexus"></param>
        /// <param name="name"></param>
        protected jNameUnique(ClientNexusBase nexus, string name)
        :base(nexus)
        {
            if (name == "" || name == null)
                throw new ArgumentException("Expecting a non empty name.");
            this.Name = name;

            if (nexus != null)
            {
                // this instance was bound ('ClientNexus.Bind()') in the base constructor;
                // however it was not added to the NamedCache because it had no name when it was bound.
                nexus.Cache.NamedObjects.Add(this);
            }
            // else
            // no need to add to NamedObject cache since this instance is added when Bound.
        }
        /// <summary>
        /// Constructor 
        /// <para>Using this constructor will require use of the 'ClientNexus.Bind(thisInstance)' method.</para>
        /// </summary>
        /// <param name="name"></param>
        protected jNameUnique(string name)
        :this(null, name)
        {
        }
        #endregion
        #region Name
        [BaseField]
        private string name;
        /// <summary>
        /// Returns a unique name for this instance of a '<see cref="jObject"/>' class.
        /// </summary>
        public override string Name
        {
            get { return this.name; }
            internal protected set
            {
                if (name == null || name == "")
                    this.name = value;
                else 
                    throw new InvalidOperationException("Can only set the name once.");
            }
        }
        #endregion
    }
}
